package cn.lrwjz.androidguide.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import cn.lrwjz.androidguide.R;

/**
 * 对话框示例Activity
 * 展示Material Design对话框组件的使用方法
 */
public class DialogActivity extends AppCompatActivity {
    private int selectedItem = 0;
    private boolean[] selectedItems = {false, false, false, false};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Dialog 示例");
        }

        // 基础对话框
        findViewById(R.id.btnBasicDialog).setOnClickListener(v -> {
            new MaterialAlertDialogBuilder(this)
                    .setTitle("基础对话框")
                    .setMessage("这是一个基础的对话框示例，包含标题、内容和按钮。")
                    .setPositiveButton("确定", (dialog, which) -> 
                        Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show())
                    .setNegativeButton("取消", (dialog, which) -> 
                        Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show())
                    .setNeutralButton("稍后再说", (dialog, which) -> 
                        Toast.makeText(this, "点击了稍后再说", Toast.LENGTH_SHORT).show())
                    .show();
        });

        // 单选对话框
        findViewById(R.id.btnSingleChoiceDialog).setOnClickListener(v -> {
            String[] items = {"选项一", "选项二", "选项三", "选项四"};
            new MaterialAlertDialogBuilder(this)
                    .setTitle("单选对话框")
                    .setSingleChoiceItems(items, selectedItem, (dialog, which) -> {
                        selectedItem = which;
                        Toast.makeText(this, 
                            "选择了: " + items[which], 
                            Toast.LENGTH_SHORT).show();
                    })
                    .setPositiveButton("确定", null)
                    .show();
        });

        // 多选对话框
        findViewById(R.id.btnMultiChoiceDialog).setOnClickListener(v -> {
            String[] items = {"选项一", "选项二", "选项三", "选项四"};
            new MaterialAlertDialogBuilder(this)
                    .setTitle("多选对话框")
                    .setMultiChoiceItems(items, selectedItems, (dialog, which, isChecked) -> {
                        selectedItems[which] = isChecked;
                        Toast.makeText(this, 
                            items[which] + (isChecked ? " 选中" : " 取消选中"), 
                            Toast.LENGTH_SHORT).show();
                    })
                    .setPositiveButton("确定", null)
                    .show();
        });

        // 输入对话框
        findViewById(R.id.btnInputDialog).setOnClickListener(v -> {
            EditText input = new EditText(this);
            input.setInputType(InputType.TYPE_CLASS_TEXT);
            input.setHint("请输入内容");
            input.setPadding(32, 32, 32, 32);

            new MaterialAlertDialogBuilder(this)
                    .setTitle("输入对话框")
                    .setView(input)
                    .setPositiveButton("确定", (dialog, which) -> {
                        String text = input.getText().toString();
                        Toast.makeText(this, 
                            "输入的内容: " + text, 
                            Toast.LENGTH_SHORT).show();
                    })
                    .setNegativeButton("取消", null)
                    .show();
        });

        // 自定义对话框
        findViewById(R.id.btnCustomDialog).setOnClickListener(v -> {
            View customView = LayoutInflater.from(this)
                    .inflate(R.layout.dialog_custom, null);

            new MaterialAlertDialogBuilder(this)
                    .setView(customView)
                    .setPositiveButton("确定", (dialog, which) -> 
                        Toast.makeText(this, "关闭自定义对话框", Toast.LENGTH_SHORT).show())
                    .show();
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 